home *** CD-ROM | disk | FTP | other *** search
- /*
- * pSmallDaemon
- *
- * 7/92 Greg Robbins based on code by C.K. Haun
- *
- * This is a minimal faceless background application for System 7.
- *
- * It demonstrates how to install and dispatch Apple events, as well
- * as the other bare essentials for a faceless background app.
- *
- * The file type for this application should be 'APPL' if it will be launched
- * like an application or 'appe' if it will be placed into the Extensions
- * folder and launched at startup. 'appe' files can also have an INIT resource
- * to put up an icon (using ShowInit) at startup.
- */
-
-
- #include <AppleEvents.h>
- #include <AERegistry.h>
-
- #include <Errors.h>
- #include <Gestalt.h>
- #include <Memory.h>
- #include <Resources.h>
- #include <TextUtils.h>
- #include <Types.h>
-
- #define kSleepMax 0
-
- // Global Variables
-
- Boolean gQuitFlag = false;
- Boolean gAppleEventsFlag = true;
- long gSleepVal = 0;
- OSErr gRetCode = noErr;
- long gGestResponse = 0;
- EventRecord gMainEventRec;
- Boolean gEventFlag = false;
-
- // Code resource routines.
-
- typedef pascal void* (*tCodeFrag)(int iRoutineSelector);
-
- typedef CALLBACK_API( OSErr , RegisterFunctionProcPtr )(OSType selector, long response);
- typedef STACK_UPP_TYPE(RegisterFunctionProcPtr) RegisterFunctionUPP;
-
- SelectorFunctionUPP gFindSelector = nil;
- RegisterFunctionUPP gRegisterSelector = nil;
-
- // { Apple event handlers to be installed }
-
- static
- pascal
- OSErr
- DoAEOpenApplication
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAEOpenApplication
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- return err;
- }
-
- static
- pascal
- OSErr
- DoAEOpenDocuments
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAEOpenDocuments
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- return err;
- }
-
- static
- pascal
- OSErr
- DoAEPrintDocuments
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAEPrintDocuments
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- return err;
- }
-
- static
- pascal
- OSErr
- DoAEQuitApplication
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAEQuitApplication
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- gQuitFlag = true;
- return err;
- }
-
- static
- pascal
- OSErr
- DoAESteve
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAESteve
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- OSType selector;
- OSType actualType;
- Size actualSize;
- long value = 0;
- long patchIndex = -1;
-
- err =
- AEGetParamPtr
- (
- theEvent,
- keyDirectObject,
- typeType,
- &actualType,
- &selector,
- sizeof(selector),
- &actualSize
- );
-
- if (err == noErr)
- {
- err =
- AEGetParamPtr
- (
- theEvent,
- 'ARF ',
- typeLongInteger,
- &actualType,
- &value,
- sizeof(value),
- &actualSize
- );
- }
-
- if (err == noErr)
- {
- err = gRegisterSelector(selector, value);
- }
-
- if (err == noErr)
- {
- err =
- AEPutParamPtr
- (
- theReply,
- keyAEResult,
- typeLongInteger,
- &value,
- sizeof(value)
- );
- }
-
- return err;
- }
-
- static
- pascal
- OSErr
- DoAEGetGestalt
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- );
-
- static
- pascal
- OSErr
- DoAEGetGestalt
- (
- const AppleEvent* theEvent,
- AppleEvent* theReply,
- long refcon
- )
- {
- OSErr err = noErr;
- OSType selector;
- OSType actualType;
- Size actualSize;
- long response = 0;
- long patchIndex = -1;
-
- err =
- AEGetParamPtr
- (
- theEvent,
- keyDirectObject,
- typeType,
- &actualType,
- &selector,
- sizeof(selector),
- &actualSize
- );
-
- if (err == noErr)
- {
- err = Gestalt(selector, &response);
- }
-
- if (err == noErr)
- {
- err =
- AEPutParamPtr
- (
- theReply,
- keyAEResult,
- typeMagnitude,
- &response,
- sizeof(response)
- );
- }
-
- return err;
- }
-
- static
- pascal
- void
- InitAppleEventsStuff
- (
- )
- {
- OSErr retCode = noErr;
-
- if (gAppleEventsFlag)
- {
- retCode =
- AEInstallEventHandler
- (
- kCoreEventClass,
- kAEOpenApplication,
- (AEEventHandlerUPP)DoAEOpenApplication,
- 0,
- false
- );
-
- if (retCode == noErr)
- {
- retCode =
- AEInstallEventHandler
- (
- kCoreEventClass,
- kAEOpenDocuments,
- (AEEventHandlerUPP)DoAEOpenDocuments,
- 0,
- false
- );
- }
-
- if (retCode == noErr)
- {
- retCode =
- AEInstallEventHandler
- (
- kCoreEventClass,
- kAEPrintDocuments,
- (AEEventHandlerUPP)DoAEPrintDocuments,
- 0,
- false
- );
- }
-
- if (retCode == noErr)
- {
- retCode =
- AEInstallEventHandler
- (
- kCoreEventClass,
- kAEQuitApplication,
- (AEEventHandlerUPP)DoAEQuitApplication,
- 0,
- false
- );
- }
-
- if (retCode == noErr)
- {
- retCode =
- AEInstallEventHandler
- (
- 'SPJ!',
- 'SPJ!',
- (AEEventHandlerUPP)DoAESteve,
- 0,
- false
- );
- }
-
- if (retCode == noErr)
- {
- retCode =
- AEInstallEventHandler
- (
- 'SPJ!',
- 'GetG',
- (AEEventHandlerUPP)DoAEGetGestalt,
- 0,
- false
- );
- }
-
- if (retCode != noErr)
- {
- DebugStr("\pInstall event handler failed");
- }
- }
- }
-
- static
- pascal
- OSErr
- DoHighLevelEvent
- (
- const EventRecord* theEventRec
- )
- {
- OSErr retCode = noErr;
-
- retCode = AEProcessAppleEvent(theEventRec);
- return retCode;
- }
-
- void main()
- {
- Handle codeFrag = nil;
- tCodeFrag fragMain;
- Size fragSize;
-
- /*
- faceless background apps only get a 2K stack by default. If necessary,
- increase the stack size here (by calling GetApplLimit to find the current
- heap limit, and SetApplLimit to set it to a lower address, thus reserving
- more space for the stack)
- */
-
- // { initialize QuickDraw globals }
-
- InitGraf(qd.thePort);
-
- // { initialize application globals }
-
- gQuitFlag = false;
- gSleepVal = kSleepMax;
-
- // { is the Apple Event Manager available? }
-
- gRetCode = Gestalt(gestaltAppleEventsAttr, &gGestResponse);
-
- if ((gRetCode == noErr) && (gGestResponse & (1L << gestaltAppleEventsPresent)))
- {
- gAppleEventsFlag = true;
- }
- else
- {
- gAppleEventsFlag = false;
- }
-
- // { install Apple event handlers }
-
- InitAppleEventsStuff();
-
- // Set up the code resource.
-
- codeFrag = GetResource('ARF!', 128);
- gRetCode = ResError();
-
- if (codeFrag && (gRetCode == noErr))
- {
- fragSize = GetHandleSize(codeFrag);
- fragMain = (tCodeFrag)NewPtrSys(fragSize);
-
- gRetCode = MemError();
- }
-
- if (gRetCode == noErr)
- {
- BlockMoveData(*codeFrag, fragMain, fragSize);
- FlushInstructionCache();
- gFindSelector = fragMain(0);
- gRegisterSelector = fragMain(1);
- }
-
- if (codeFrag)
- {
- ReleaseResource(codeFrag);
- }
-
- if ((!gFindSelector) || (!gRegisterSelector))
- {
- gQuitFlag = true;
- }
-
- // { main event loop }
-
- while (!gQuitFlag)
- {
- gEventFlag =
- WaitNextEvent(highLevelEventMask, &gMainEventRec, gSleepVal, nil);
-
- // { faceless background tasks receive only high-level events }
- if (gMainEventRec.what == kHighLevelEvent)
- {
- DoHighLevelEvent(&gMainEventRec);
- }
-
- // { during testing, I like to call GetKeys here and check if the CapsLock
- // key is down. If it is, set gQuitFlag so the program will exit. }
- }
-
- }
-